package com.controller;

import com.service.SmsService;
import com.service.exception.SmsCodeException;
import com.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HomeController {

    @Autowired
    private SmsService smsService;

    @RequestMapping("")
    public String index(){
        return "index";
    }

    @RequestMapping("/code")
    @ResponseBody
    public ResponseVO<String> code(){
        String code = smsService.generateCode();
        System.out.println("code:========: " + code);
        ResponseVO<String> responseVO = new ResponseVO<>(200,"ok",code);

        return responseVO;
    }


    @RequestMapping("/login")
    @ResponseBody
    public ResponseVO<Boolean> login(String code){

        boolean validCode = false;
        try {
            validCode = smsService.checkCode(code);
        } catch (SmsCodeException sce) {
            sce.printStackTrace();
            ResponseVO<Boolean> responseVO = new ResponseVO<>(10001,sce.getMessage(),null);
        }
        System.out.println("code:========: " + code + " is valid? " + validCode);
        String msg = validCode ? "ok" : "请重新获取校验码";
        ResponseVO<Boolean> responseVO = new ResponseVO<>(200,msg,validCode);

        return responseVO;
    }
}
